package com.example.util;

import android.app.Activity;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;

import com.example.baselibrary.R;
import com.example.mvp.bean.BannerDataEntity;

import java.util.ArrayList;

import cc.vv.lkbasecomponent.util.LKScreenUtil;


/**
 * Banner关类
 * 
 * @author CHP
 * 
 */

public class DetailImgurlsUtilMove {

	private ArrayList<BannerDataEntity> listData;
	private LinearLayout linerlayout_movie_point;
	private Activity mActivity;
	private Handler mHandler;
	private ViewPager ADviewpager;

   private boolean isRunning;
	private PriorityExecutor priorityExecutor;
	private Runnable runnable;

	public DetailImgurlsUtilMove(  LinearLayout linerlayout_movie_point, Activity context,
								   Handler mHandler, ViewPager ADviewpager) {
		// TODO Auto-generated constructor stub
		this.linerlayout_movie_point = linerlayout_movie_point;
		this.mActivity = context;
		this.mHandler = mHandler;
		this.ADviewpager = ADviewpager;
	}
	/**
	 * 添加轮播点
	 */
	public void addViewpgPoint(ArrayList<BannerDataEntity> listData) {
		if (listData==null||linerlayout_movie_point==null){
			return;
		}
		this.listData = listData;
		linerlayout_movie_point.removeAllViews();
		if (mActivity == null) {
			return;
		}
		if (listData.size()<=1){
			return;
		}
		LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LKScreenUtil.dp2px(4),LKScreenUtil.dp2px(4));
		for (int i = 0; i < listData.size(); i++) {
			View v=new View(mActivity);
			if (layoutParams!=null){
				v.setBackground(ContextCompat.getDrawable(mActivity, R.drawable.selector_pager_point));
				v.setLayoutParams(layoutParams);
			}
			layoutParams.setMargins(LKScreenUtil.dp2px(6),0,0,0);
			linerlayout_movie_point.addView(v);
		}
		changeColorVpgPoint(0);
		setTimeData();
		viewpagerTouch();
	}

	/**
	 * 设置轮播点
	 * @param index
	 */
	public void changeColorVpgPoint(int index) {
		for (int i = 0; i < listData.size(); i++) {
			View view = linerlayout_movie_point
					.getChildAt(i);
			if (view != null&&i==index) {
				view.setSelected(true);
			}else{
				view.setSelected(false);
			}
		}
	}

	/**
	 * 设置时间
	 */
	private void setTimeData() {
		if (mHandler==null){
			return;
		}
		isRunning=true;
		priorityExecutor = new PriorityExecutor(5, true);
		runnable = new Runnable() {
			@Override
			public void run() {
				while (isRunning) {
					SystemClock.sleep(5000);
					mHandler.sendEmptyMessage(100);
				}
			}
		};
		priorityExecutor.execute(runnable);

	}

	/**
	 * 销毁轮播
	 */
	public void destoryRunning(){
		if (priorityExecutor!=null&&runnable!=null){
			isRunning=false;
			priorityExecutor.remove(runnable);
		}
	}

	/**
	 * 停止轮播
	 */
	public void stopRunning(){
			isRunning=false;
	}

	/**
	 * 开始轮播
	 */
	public void startRunning(){
		isRunning=true;
		if(priorityExecutor!=null&&runnable!=null) {
			if (!priorityExecutor.isBusy()) {
				priorityExecutor.execute(runnable);
			}
		}
	}

	/**
	 * 触摸时让线程停止或开始
	 */
	private void viewpagerTouch() {
		if (ADviewpager==null){
			return;
		}
		ADviewpager.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// 触摸时停止滚动
				if (event.getAction() == MotionEvent.ACTION_UP) {
						isRunning=true;
				} else {
					isRunning=false;
				}
				return false;
			}
		});
	}

}
